package com.game.server.message.proto.execute;

import com.comm.ISession;

import com.game.majiang.config.DataManager;
import com.game.majiang.logic.LogicClient;
import com.game.majiang.logic.LogicServer;
import com.game.server.db.entity.UserInfoData;
import com.game.server.db.entity.XyClubInfo;
import com.game.server.db.entity.XyClubMemberInfo;
import com.game.server.message.proto.in.AbstractXYmClubplayersReq;
import com.game.server.message.proto.out.XYclubplayerStruct;
import com.game.server.message.proto.out.XYmClubplayersRsp;
import com.game.server.util.Util;
import com.sun.org.apache.bcel.internal.generic.IF_ACMPEQ;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.List;

public class XYmClubplayersReq extends AbstractXYmClubplayersReq {

    @Override
    public void Execute(ISession session) {
        int limit = 15;
        LogicClient client = (LogicClient) LogicServer.getInstance().getClient(session.getId());
        if (client != null&&client.checkClick(msgcode)) {
            XYmClubplayersRsp rsp = new XYmClubplayersRsp();
            //获取俱乐部信息
            XyClubInfo clubInfo = DataManager.getInstance().getMapClub().get(this.clubid.intValue());
            if (clubInfo != null) {
                rsp.setPage(this.page);
                ArrayList<XyClubMemberInfo> players = clubInfo.getPlayers();
                List<XYclubplayerStruct> list = new ArrayList<>();
                for (XyClubMemberInfo entity : players) {
                    if (entity.getDel_type()==1){
                        continue ;
                    }
                    if (this.searchVal!=null && !"".equals(this.searchVal)){
                        String name = DataManager.getInstance().getMapUserData().get(entity.getUid()).name;
                        String[] searchs = new String[2];
                        searchs[0] = name;
                        searchs[1] = entity.getUid()+"";
                        if (!Util.useSetArr(searchs,this.searchVal)){
                            continue ;
                        }
                    }
                    UserInfoData infoData = DataManager.getInstance().getMapUserData().get(entity.getUid());
                    XYclubplayerStruct yclubplayerStruct = new XYclubplayerStruct();
                    //用户签名
                    yclubplayerStruct.setContent("");
                    yclubplayerStruct.setGongxianbili(entity.getGongxianbili());
                    yclubplayerStruct.setHeadurl(infoData.getHead());
                    yclubplayerStruct.setId(entity.getUid());
                    yclubplayerStruct.setIsManager(entity.getIsManager());
                    yclubplayerStruct.setName(infoData.getName());
                    yclubplayerStruct.setSex(infoData.getSex());
                    yclubplayerStruct.setXingyuvalue(entity.getXingyuvalue());

                    LogicClient toClient = (LogicClient) LogicServer.getInstance().getClient(entity.getUid());
                    if (toClient!=null){
                        yclubplayerStruct.setLoginouttime(-1);
                    }else{
                        UserInfoData toUserInfoData = DataManager.getInstance().getMapUserData().get(entity.getUid());

                        long l = System.currentTimeMillis() - 10000;
                        if (toUserInfoData.getLogintime()!=null){
                            l = toUserInfoData.getLogintime().getTime();
                        }
                        long outTime = (System.currentTimeMillis()-l)/1000;
                        if (outTime>0){
                            yclubplayerStruct.setLoginouttime(Math.toIntExact(outTime));
                        }else{
                            yclubplayerStruct.setLoginouttime(1);
                        }
                    }
                    list.add(yclubplayerStruct);
                }
                int sumCount = list.size();
                int sumPage = (int) Math.ceil((double) sumCount/limit);
                page = page<1?1:page>sumPage?sumPage:page;
                page = page<1?1:page;
                int beginIndex = ((page-1)*limit);

                rsp.setAllPlayercount(clubInfo.getPlayerCount());
                rsp.setAllpage(sumPage);
                rsp.setOnlineCount(clubInfo.getOnlineCount());
                rsp.setPlayerallxinyu(clubInfo.getTotalContribution()+"");
                rsp.setHasxinyuvaluecount(clubInfo.getNumberOfPeopleWithQuota());
                list.sort(Comparator.comparing(XYclubplayerStruct::getIsManager).thenComparing(XYclubplayerStruct::getLoginouttime));
                List<XYclubplayerStruct> sendList = new ArrayList<>();
                int endIndex = 1;
                for (int i = 0; i < list.size(); i++) {
                    if (i>=beginIndex && endIndex<=limit){
                        XYclubplayerStruct userListStructrue = list.get(i);
                        sendList.add(userListStructrue);
                        endIndex++;
                    }
                }

                //list转数组
                XYclubplayerStruct[] clubUserList = sendList.toArray(new XYclubplayerStruct[0]);
                rsp.setPlayers(clubUserList);
            }
            client.Send(rsp);
        }
    }

}
